const db = require("../../db/index");
exports.create = (req, res) => {
  const sensorInfo = req.body;
  if (!sensorInfo.term_deveui || !sensorInfo.cell_type) {
    return res.send({
      status: 1,
      message: "创建传感器数据记录失败，确实终端deveui或生产单元ID",
    });
  }

  let sql_query = `select id from cell_list where type = '${sensorInfo.cell_type}'`;
  db.query(sql_query, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "指定的生产单元不存在",
      });
    }
    let sql_insert = `insert into sensor_list set ?`;
    db.query(
      sql_insert,
      {
        term_deveui: sensorInfo.term_deveui ? sensorInfo.term_deveui : null,
        cell_id: result[0].id ? result[0].id : null,
        address: sensorInfo.address ? sensorInfo.address : null,
        numbers: sensorInfo.numbers ? sensorInfo.numbers : null,
        type_data_id: sensorInfo.type_data_id ? sensorInfo.type_data_id : null,
      },
      (err, ress) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (ress.affectedRows !== 1) {
          return res.send({
            status: 1,
            message: "传感器添加失败，请稍后再试！",
          });
        }
        res.send({ status: 0, message: "添加成功！" });
      }
    );
  });
};
exports.deleteBySensorId = (req, res) => {
  const sensor_id = req.query.sensor_id;
  if (!sensor_id) {
    return res.send({
      status: 1,
      message: "传感器id缺失",
    });
  }
  let sql_query = `select * from sensor_list where id = ?`;
  db.query(sql_query, sensor_id, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "删除单元不存在",
      });
    }
    let sql_delete = "delete from sensor_list where id = ?";
    db.query(sql_delete, sensor_id, (err, results) => {
      if (err) {
        return res.send({
          status: 2,
          message: err.message,
        });
      }
      if (results.affectedRows != 1) {
        return res.send({
          status: 1,
          message: "删除失败，请稍后重试!",
        });
      }
      return res.send({
        status: 0,
        message: "删除成功",
      });
    });
  });
};

exports.updateBySensorId = (req, res) => {
  const sensorInfo = req.body;

  if (!sensorInfo.sensor_id) {
    return res.send({
      status: 1,
      message: "传感器id缺失",
    });
  }

  let sql_query = `select * from sensor_list where id = ?`;

  db.query(sql_query, sensorInfo.sensor_id, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "修改传感器不存在",
      });
    }

    let sql_update = "update sensor_list set ? where id = ?";
    db.query(
      sql_update,
      [
        { cell_id: sensorInfo.cell_id, address: sensorInfo.address },
        sensorInfo.sensor_id,
      ],
      (err, results) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (results.affectedRows != 1) {
          return res.send({
            status: 1,
            message: "修改失败，请稍后重试!",
          });
        }
        return res.send({
          status: 0,
          message: "修改成功",
        });
      }
    );
  });
};

exports.getBySensorId = (req, res) => {
  const sensorId = req.query.sensor_id;

  if (!sensorId) {
    return res.send({
      status: 1,
      message: "传感器id缺失",
    });
  }
  let sql_query = "select * from sensor_list where id =?";
  db.query(sql_query, sensorId, (err, results) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    return res.send({
      status: 0,
      message: "获取成功",
      data: results,
    });
  });
};
exports.getAll = (req, res) => {
  let sql_query = "select * from cell_list";
  db.query(sql_query, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    return res.send({
      status: 0,
      total: result.length,
      message: "获取成功",
      data: result,
    });
  });
};
